package com.atguigu.paymentdemo.config;

import com.alipay.api.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @author longge
 * @Title:
 * @Description:
 * @date 2023/2/13 21:01
 */
@Configuration
@PropertySource("classpath:alipay-sandbox.properties")
@ConfigurationProperties(prefix = "alipay")
@Data
public class AlipayClientConfig {

    /**
     * appid
     */
    private String appId;

    /**
     * 商户的PID
     */
    private String sellerId;

    /**
     * 支付宝网关
     */
    private String gatewayUrl;

    /**
     * 应用私钥
     */
    private String merchantPrivateKey;

    /**
     * 阿里公钥
     */
    private String alipayPublicKey;

    /**
     * 对称秘钥
     */
    private String contentKey;

    private String returnUrl;

    /**
     * 回调地址
     */
    private String notifyUrl;

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(gatewayUrl);
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(merchantPrivateKey);
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
//构造client
        return new DefaultAlipayClient(alipayConfig);
    }
}
